//package com.cqc.manage.aop;
//
//import com.cqc.manage.dto.PayloadDto;
//import com.cqc.manage.entity.User;
//import com.cqc.manage.error.ErrorEnum;
//import com.cqc.manage.error.tokenerror.TokenException;
//import com.cqc.manage.service.Imp.RoleService;
//import com.cqc.manage.service.Imp.user.UserService;
//import com.cqc.manage.util.JdbcTemplateSql;
//import com.cqc.manage.token.JwtTokenService;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.annotation.AfterReturning;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Before;
//import org.aspectj.lang.annotation.Pointcut;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.ArrayList;
//import java.util.List;
//
//@Aspect
//@Component
//public class ControllerAuthorityAop {
//
//    static private Logger LOG = LoggerFactory.getLogger(ControllerAuthorityAop.class);
//
//    @Autowired
//    private JwtTokenService jwtTokenService;
//    @Autowired
//    private RoleService roleService;
//    @Autowired
//    private UserService userService;
//    @Autowired
//    private JdbcTemplateSql jdbcTemplateSql;
//
//    @Pointcut("execution(public * com.cqc.manage.controller.admin..*.*(..))")
//    public void UserControllerPoint(){
//    }
//
//    /**
//     * 前置通知：在连接点之前执行的通知
//     * @param joinPoint
//     * @throws Throwable
//     */
//    @Before("UserControllerPoint()")
//    public void doBefore(JoinPoint joinPoint) throws Throwable {
//        List<String> userRole = new ArrayList<>();
//        // 接收到请求，记录请求内容
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        if (request.getServletPath().contains("login")) return;
//        String authHeader = request.getHeader("Authorization");
//
//        // 获取token
//        String token = authHeader.substring(7);
//        // 验证token
//        PayloadDto test = jwtTokenService.verifyTokenByHMAC(token);
//        User byUserName = userService.findByUserName(test.getUserName());
//        List<String> strings = jdbcTemplateSql.UserRoleConn(byUserName.getUserId());
//        for (String str : strings){
//            userRole.add(roleService.selectById(str).getRoleName());
//        }
//        if (!userRole.contains("admin")){
//            throw new TokenException("P001", ErrorEnum.NOPERMISSION.getCode());
//        }
//    }
//
//    @AfterReturning(returning = "ret",pointcut = "UserControllerPoint()")
//    public void doAfterReturning(Object ret) throws Throwable {
//        // 处理完请求，返回内容
//        LOG.info("RESPONSE : " + ret);
//    }
//
//}
